package org.bonorsoft.modules.mobile.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.constant.UserConstants;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.DateUtils;
import org.bonorsoft.modules.mobile.entity.MobileDict;
import org.bonorsoft.modules.mobile.service.IMobileDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@Slf4j
@Controller
@RequestMapping("/mobile/dict")
public class MobileDictController extends BonorController<MobileDict, IMobileDictService> {
    @Autowired
    private IMobileDictService mobileDictService;

    private String prefix = "mobile/dict/type";

    @RequiresPermissions("mobile:dict:view")
    @GetMapping()
    public String dictType()
    {
        return prefix + "/type";
    }

    /**
     * 分页列表查询
     *
     * @param mobileDict
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "字典类别-分页列表查询")
    @GetMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public Result<?> queryPageList(MobileDict mobileDict,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<MobileDict> queryWrapper = QueryGenerator.initQueryWrapper(mobileDict, req.getParameterMap());
        Page<MobileDict> page = new Page<MobileDict>(pageNo, pageSize);
        IPage<MobileDict> pageList = mobileDictService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 新增字典类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 添加
     *
     * @param mobileDict
     * @return
     */
    @AutoLog(value = "字典类别-添加")
    @RequiresPermissions("mobile:dict:add")
    @PostMapping(value = "/add")
    @ResponseBody
    public Result<?> add(@Validated MobileDict mobileDict) {
        if (UserConstants.DICT_TYPE_NOT_UNIQUE.equals(mobileDictService.checkDictTypeUnique(mobileDict)))
        {
            return Result.error("新增字典'" + mobileDict.getDictName() + "'失败，字典类型已存在");
        }
        mobileDict.setCreateBy(getLoginUserName());
        mobileDict.setCreateTime(DateUtils.getDate());
        mobileDictService.save(mobileDict);
        return Result.ok("添加成功！");
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("dict", mobileDictService.getById(id));
        return prefix + "/edit";
    }

    /**
     * 编辑
     *
     * @param mobileDict
     * @return
     */
    @AutoLog(value = "字典类别-编辑")
    @PostMapping(value = "/edit")
    @RequiresPermissions("mobile:dict:edit")
    @ResponseBody
    public Result<?> edit(@Validated MobileDict mobileDict) {
        if (UserConstants.DICT_TYPE_NOT_UNIQUE.equals(mobileDictService.checkDictTypeUnique(mobileDict)))
        {
            return Result.error("修改字典'" + mobileDict.getDictName() + "'失败，字典类型已存在");
        }
        mobileDict.setUpdateBy(getLoginUserName());
        mobileDict.setUpdateTime(DateUtils.getDate());
        mobileDictService.updateById(mobileDict);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "字典类别-通过id删除")
    @DeleteMapping(value = "/delete")
    @ResponseBody
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        mobileDictService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "字典类别-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    @RequiresPermissions("mobile:dict:remove")
    @ResponseBody
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.mobileDictService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "字典类别-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        MobileDict mobileDict = mobileDictService.getById(id);
        return Result.ok(mobileDict);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param mobileDict
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MobileDict mobileDict) {
        return super.exportXls(request, mobileDict, MobileDict.class, "字典类别");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MobileDict.class);
    }

    /**
     * 校验字典类型
     */
    @PostMapping("/checkDictTypeUnique")
    @ResponseBody
    public String checkDictTypeUnique(MobileDict dictType)
    {
        return mobileDictService.checkDictTypeUnique(dictType);
    }

    /**
     * 查询字典详细
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("dict", mobileDictService.getById(id));
        mmap.put("dictList", mobileDictService.list());
        return "mobile/dict/data/data";
    }

    /**
     * 清空缓存
     */
    @RequiresPermissions("mobile:dict:remove")
    @AutoLog(value = "字典类别-清空缓存")
    @GetMapping("/clearCache")
    @ResponseBody
    public Result<?> clearCache()
    {
        mobileDictService.clearCache();
        return Result.ok("清空缓存成功！");
    }


}
